const formatNumber = n => {
  n = n.toString()
  return n[1] ? n : `0${n}`
}

export const formatTime = date => {
  const year = date.getFullYear()
  const month = date.getMonth() + 1
  const day = date.getDate()
  const hour = date.getHours()
  const minute = date.getMinutes()
  const second = date.getSeconds()
  // ${[hour, minute, second].map(formatNumber).join(':')}
  return `${[year, month, day].map(formatNumber).join('-')}`
}

export const sum = (list) => {
  return list.reduce((accumulator, currentValue) => accumulator + currentValue, 0);
}




export function timestampToTime(value, type = 0) {
  var time = new Date(parseInt(value));
  var year = time.getFullYear();
  var month = time.getMonth() + 1;
  var date = time.getDate();
  var hour = time.getHours();
  var minute = time.getMinutes();
  var second = time.getSeconds();
  month = month < 10 ? "0" + month : month;
  date = date < 10 ? "0" + date : date;
  hour = hour < 10 ? "0" + hour : hour;
  minute = minute < 10 ? "0" + minute : minute;
  second = second < 10 ? "0" + second : second;
  var arr = [
    year + "-" + month + "-" + date,
    year + "-" + month + "-" + date + " " + hour + ":" + minute + ":" + second,
    year + "年" + month + "月" + date,
    year + "年" + month + "月" + date + " " + hour + ":" + minute + ":" + second,
    hour + ":" + minute + ":" + second
  ]
  return arr[type];
}

export function getHSTime(time) {
  if (time <= 0) {
    return 0
  }
  const hours = Math.floor(time / 3600);
  const minutes = Math.floor((time % 3600) / 60);

  // 返回格式化的时间字符串  
  return `${hours}小时${minutes}分钟`;
}

export function generateUniqueString(length) {
  let result = '';
  let characters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';
  let charactersLength = characters.length;
  for (let i = 0; i < length; i++) {
    let randomIndex = Math.floor(Math.random() * charactersLength);
    result += characters.charAt(randomIndex);
  }
  return result;
}

export const getParameterByName = (name, url) => {
  const params = url.split(`${name}=`)[1];
  return params ? params.split('&')[0] : '';
}

export const punctuationRegex = /^[!"#$%&'()*+,-./:;<=>?@[\]^_`{|}~]+|[!"#$%&'()*+,-./:;<=>?@[\]^_`{|}~]+$/g;

export function parseTime(timeStr) {  
  // 将时间字符串（如 "HH:mm"）转换为分钟数（为了简化比较）  
  const [hours, minutes] = timeStr.split(':').map(Number);  
  return hours * 60 + minutes;  
}  

function isValidTimeRange(startTime, endTime) {  
  // 检查开始时间不大于结束时间，并且开始时间不等于结束时间  
  return parseTime(startTime) < parseTime(endTime) && startTime !== endTime;  
}  

export function checkTimeOverlap(timeRanges) {  
  // 辅助函数，检查两个时间段是否重叠  
  function overlaps(range1, range2) {  
      const [start1, end1] = range1;  
      const [start2, end2] = range2;  
      return parseTime(start1) < parseTime(end2) && parseTime(start2) < parseTime(end1);  
  }  

  // 检查所有时间段是否有效  
  for (let range of timeRanges) {  
      const [start, end] = range;  
      if (!isValidTimeRange(start, end)) {  
          return 1; // 如果有无效的时间段，立即返回false  
      }  
  }

  // 检查时间段重叠  
  for (let i = 0; i < timeRanges.length; i++) {  
      for (let j = i + 1; j < timeRanges.length; j++) {  
          if (overlaps(timeRanges[i], timeRanges[j])) {  
              return 2; // 如果有重叠，立即返回false  
          }  
      }  
  }  

  // 如果没有错误且没有重叠，则返回true  
  return 0;  
}